package com.example.demo;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;

import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author: jiawp
 * @date: 2024/1/12 10:48
 * @description:
 **/
public class Aoo {

    public static void main(String[] args) throws Exception {
        //初始化所有表格
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        Configure config = Configure.builder()
                .bind("relativeList", policy).
                build();

        List<PersonExport> relativeList =new ArrayList<>();
        PersonExport person =new PersonExport();
        person.setCounterpartyNameStr("相对人");
        person.setCounterpartyName("李明");
        person.setCodeStr("相对人编码");
        person.setCode("43352650");
        person.setAuthorStr("法定代表人");
        person.setAuthor("张三");
        person.setContactPersonStr("联系人/代理人");
        person.setContactPerson("孙红");
        person.setPhoneStr("联系方式");
        person.setPhone("15155163721");
        person.setAdressStr("地址/邮编");
        person.setAdress("安徽省合肥市金寨路96号");
        relativeList.add(person);
        relativeList.add(person);

        Boo boo=new Boo();
        boo.setA("aaaa");
        boo.setB("bbbbb");
        boo.setC("cccccc");
        boo.setD("dddddd");

        PersonExport person1 =new PersonExport();
        person1.setAdress("地址");
        person1.setPhone("123213123");

        TbContractMangerDetailVo tbContractMangerDetailVo =new TbContractMangerDetailVo();
        tbContractMangerDetailVo.setChangeMatter("0,1,2,3");
        tbContractMangerDetailVo.setAmountOriginal(new BigDecimal(1231232133));
        tbContractMangerDetailVo.setAmountLast(new BigDecimal(565));
        tbContractMangerDetailVo.setAmountNow(new BigDecimal(434));
        tbContractMangerDetailVo.setTaxRate("20%");
        tbContractMangerDetailVo.setAmountNowNoRate(new BigDecimal(23234134));
        tbContractMangerDetailVo.setAmountNowRateSub(new BigDecimal(2312));
        String changeDetails = getChangeDetails(tbContractMangerDetailVo);

        XWPFTemplate template = XWPFTemplate.compile("src/main/resources/relativeList3.docx",config).render
                (new HashMap<String, Object>(){{
//            put("relativeList", "Poi-tl 模板引擎");
                    put("relativeList",relativeList);
                    put("relativeList1",relativeList);
                    put("boo",boo);
                    put("person",person1);
                    put("tbContractMangerDetailVo",changeDetails);
        }});
        FileOutputStream out = new FileOutputStream("src/main/resources/out_template.docx");
        template.write(out);
        out.flush();
        out.close();
        template.close();
        System.out.println("转换完毕");
    }

    /**
     * 变更内容拼接字符串 方法
     */
    private static String getChangeDetails(TbContractMangerDetailVo mangerDetailVo) {
        if (mangerDetailVo == null) {
//            throw new GlobalException("不存在该变更内容");
        }
        String changeMatter = mangerDetailVo.getChangeMatter();
        //变更事项(0:标的总金额（含税）;1:履行期限；2:主体名称;3:其他)
        if (changeMatter == null) {
//            throw new GlobalException("不存在该变更内容");
        }
        StringBuilder st = new StringBuilder();
        if (changeMatter.contains("0")) {
            st.append("原标的总金额(含税)").append("\t").append(mangerDetailVo.getAmountOriginal()).append("\n");
            st.append("上次变更后标的总金额(含税)").append("\t").append(mangerDetailVo.getAmountLast()).append("\n");
            st.append("本次变更后标的总金额(含税)").append("\t").append(mangerDetailVo.getAmountNow()).append("人民币").append("\n");
            st.append("税率").append("\t").append(mangerDetailVo.getTaxRate()).append("\n");
            st.append("本次变更后不含税总金额").append("\t").append(mangerDetailVo.getAmountNowNoRate()).append("\n");
            st.append("本次变更后总税金").append("\t").append(mangerDetailVo.getAmountNowRateSub()).append("\n");
            st.append("本次变更后标的总金额(含税)大写").append("\t").append(mangerDetailVo.getAmountNowCapital()).append("\n");
            st.append("本次变更后不含税总金额大写").append("\t").append(mangerDetailVo.getAmountNowNoRateCapital()).append("\n");
            st.append("本次变更后总税金(大写)").append("\t").append(mangerDetailVo.getAmountNowRateSubCapital()).append("\n");
            st.append("与上次变更后金额比较差额(含税)").append("\t").append(mangerDetailVo.getAmountDifferenceLast()).append("\n");
            st.append("与原合同金额比较差额(含税)").append("\t").append(mangerDetailVo.getAmountDifferenceOriginal()).append("\n");
        } else if (changeMatter.contains("1")) {
            st.append("原履行期限").append(mangerDetailVo.getPerformStartTimeOriginal()).append("至").append(mangerDetailVo.getPerformEndTimeOriginal()).append("\n");
            st.append("变更为").append(mangerDetailVo.getPerformStartTime()).append("至").append(mangerDetailVo.getPerformEndTime());
        } else if (changeMatter.contains("2")) {
//            List<TbContractChangeRecordRelativePerson> relativePersonList = mangerDetailVo.getRelativePersonList();

        } else if (changeMatter.contains("3")) {
            st.append("其他").append(" ").append(mangerDetailVo.getOtherDescription());
        }
        return st.toString();
    }

}
